package com.wang.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wang.entity.User;
import com.wang.mapper.UserMapper;
import com.wang.service.UserService;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Classname UserServiceImpl
 * @Description TODO
 * @Date 2024/9/25 14:50
 * @Created by pc
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public ResponseUtil findAll() {
        List<User> userList = userMapper.findAll();
        return ResponseUtil.get(ResponseEnum.USER_ALL_SUCCESS,userList);
    }

    @Override
    public ResponseUtil find(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return ResponseUtil.get(ResponseEnum.USER_FIND_SUCCESS,pageInfo);
    }

    @Override
    public ResponseUtil findById(Integer id) {
        User user = userMapper.findById(id);
        return ResponseUtil.get(ResponseEnum.USER_ID_SUCCESS,user);
    }

    @Override
    public ResponseUtil addUser(User user) {
        Integer result = userMapper.addUser(user);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS)
                : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil apiLogin(User user) {
        // 根据用户名获取到用户全部信息
        User loginUser = userMapper.findByUsername(user.getUsername());
        ResponseUtil r = null;
        // 密码的比对
        if(null == loginUser){
            // 用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);
        }else if(!loginUser.getPassword().equals(SaSecureUtil.md5BySalt(user.getPassword(),loginUser.getSalt()))){
            // 用户名存在 密码不匹配
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        }else if(loginUser.getRole() != 1){
            // 管理员不能登录前台
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_3);
        }else{
            // 登录成功
            // 密码置空
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_4,loginUser);
        }
        return r;
    }

    @Override
    public ResponseUtil apiAddUser(User user) {
        user.setEmail("1101110@qq.com");
        user.setQuestion("问题");
        user.setAnswer("答案");
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        user.setSalt(salt);
        // 设置用户存入的密码 前台传过来的(1次加密) + 盐 二次加密后的密码
        user.setPassword(SaSecureUtil.md5BySalt(user.getPassword(),salt));
        Integer result = userMapper.addUser(user);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS)
                : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil adminLogin(User user) {
        User loginUser = userMapper.findByUsername(user.getUsername());
        ResponseUtil r = null;
        // 密码的比对
        if(null == loginUser){
            // 用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);
        }else if(!loginUser.getPassword().equals(SaSecureUtil.md5BySalt(user.getPassword(),loginUser.getSalt()))){
            // 用户名存在 密码不匹配
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        }else if(loginUser.getRole() != 0){
            // 管理员不能登录前台
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_ERROR_ROLE);
        }else{
            // 登录成功
            // 密码置空
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_ADMIN_LOGIN_SUCCESS,loginUser);
        }
        return r;
    }
}
